<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类别" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类别" style="width: 160px" clearable>
          <el-option
            v-for="dict in dev_dataset_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据集" prop="searchValue">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="请输入数据集名称/代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" style="width: 160px" clearable>
          <el-option
            v-for="dict in common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['dev:dataset:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="datasetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="类别" align="center" prop="type"  width="80px">
        <template #default="scope">
          <dict-tag :options="dev_dataset_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="数据集代码" align="center" prop="code"  min-width="180px">   
        <template #default="scope">
          <router-link :to="'/dev/dataset/data/'+scope.row.code" class="link-type">
            <span>{{ scope.row.code }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="数据集名称" align="center" prop="name"  width="360px"/>
      <el-table-column label="状态" align="center" prop="status"  width="60px">
        <template #default="scope">
          <dict-tag :options="common_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="匹配方式" align="center" prop="searchExp"  width="80px">
        <template #default="scope">
          <dict-tag :options="sql_express" :value="scope.row.searchExp"/>
        </template>
      </el-table-column>
      <el-table-column label="搜索标题" align="center" prop="searchTitle" />
      <el-table-column label="搜索来源" align="center" prop="searchDs" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  width="240px">
        <template #default="scope">
          <el-button link type="primary" icon="CopyDocument" @click="handleCopy(scope.row)" v-hasPermi="['dev:dataset:add']">复制</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['dev:dataset:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['dev:dataset:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据集对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="datasetRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="类别" prop="type">
          <el-select v-model="form.type" placeholder="请选择类别" style="width: 160px">
            <el-option
              v-for="dict in dev_dataset_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入数据集代码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入数据集名称" />
        </el-form-item>
        <el-form-item label="查询" prop="sqls">
          <el-input v-model="form.sqls" type="textarea" :rows="5" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in common_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入数据说明" />
        </el-form-item>
        <el-form-item label="搜索匹配" prop="searchExp" >
          <el-select v-model="form.searchExp" placeholder="请选择匹配方式" style="width: 160px" clearable>
            <el-option
              v-for="dict in sql_express"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <template v-if="form.searchExp != '0'">
        <el-form-item label="搜索标题" prop="searchTitle" >
          <el-input v-model="form.searchTitle" placeholder="请输入搜索标题" />
        </el-form-item>
        <el-form-item label="搜索字段" prop="searchField">
          <el-input v-model="form.searchField" placeholder="请输入搜索字段" />
        </el-form-item>
        <el-form-item label="搜索来源" prop="searchDs">
          <el-input v-model="form.searchDs" placeholder="数据集代码/date/dagarange" />
        </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Dataset">
import { listDataset, getDataset, delDataset, addDataset, updateDataset } from "@/api/dev/dataset";

const { proxy } = getCurrentInstance();
const { sql_express, dev_dataset_type, common_status } = proxy.useDict('sql_express', 'dev_dataset_type', 'common_status');

const datasetList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'DESC',
    searchValue: null,
    type: null,
    code: null,
    name: null,
    status: null,
  },
  rules: {
    type: [
      { required: true, message: "类别不能为空", trigger: "change" }
    ],
    code: [
      { required: true, message: "数据集代码不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "数据集名称不能为空", trigger: "blur" }
    ],
    sqls: [
      { required: true, message: "查询表达式不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询数据集列表 */
function getList() {
  loading.value = true;
  listDataset(queryParams.value).then(response => {
    datasetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    type: 'list',
    code: null,
    name: null,
    sqls: null,
    status: '1',
    remark: null,
    searchTitle: null,
    searchField: null,
    searchExp: '0',
    searchDs: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null
  };
  proxy.resetForm("datasetRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据集";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDataset(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改数据集";
  });
}

/** 复制 */
function handleCopy(row) {
  let data = {...row};
  data.id = null;
  data.code = data.code+'_copy';
  data.name = data.name+'_复制';
  data.updateTime = null;
  data.updateBy = null;
  form.value = data;
  open.value = true;
  title.value = "复制数据集";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["datasetRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDataset(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDataset(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除选中的数据集，删除后引用的功能页面或者统计报表会显示异常？').then(function() {
    return delDataset(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('dev/dataset/export', {
    ...queryParams.value
  }, `dataset_${new Date().getTime()}.xlsx`)
}

getList();
</script>
